/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudget.client.general;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

import finbudget.client.ui.table.Record;

public abstract class PropertiesContainer {

	protected abstract HashMap<String, Serializable> getContainer();
	protected abstract void createContainer();

	public void setValue(String key, Serializable value) {
		if (getContainer() == null) {
			createContainer();
		}
		if (getContainer() != null) {
			getContainer().put(key, value);
		}
	}

	public Serializable getValue(String key) {
		if (key != null && getContainer() != null) {
			return getContainer().get(key);
		} else {
			return null;
		}
	}

	public String getValueString(String key) {
		Serializable result = getValue(key);
		if (result != null) {
			if (result instanceof String) {
				return (String) result;
			}
			try {
				return result.toString();
			} catch(Exception e) {
				// Intentionally left empty block
			}
		}
		return null;
	}

	public Long getValueLong(String key) {
		Serializable value = getValue(key);
		if (value != null) {
			if (value instanceof Long) {
				return (Long) value;
			} else if (value instanceof String) {
				try {
					return Long.parseLong((String) value);
				} catch (Exception e) {
				}
			}
		}
		return null;
	}

	public Boolean getValueBoolean(String key) {
		Serializable value = getValue(key);
		if (value != null) {
			if (value instanceof Boolean) {
				return (Boolean) value;
			} else if (value instanceof String) {
				try {
					return Boolean.parseBoolean((String) value);
				} catch (Exception e) {
				}
			}
		}
		return null;
	}

	public BigDecimal getValueBigDecimal(String key) {
		Serializable value = getValue(key);
		if (value != null) {
			if (value instanceof BigDecimal) {
				return (BigDecimal) value;
			} else if (value instanceof String) {
				try {
					return new BigDecimal((String) value);
				} catch (Exception e) {
				}
			}
		}
		return null;
	}

	public ArrayList<Record> getValueArrayListOfRecords(String key) {
		Serializable value = getValue(key);
		if (value != null) {
			if (value instanceof ArrayList) {
				ArrayList<?> list = (ArrayList<?>) value;
				ArrayList<Record> records = new ArrayList<Record>(list.size());
				for (Object obj : list) {
					if (obj instanceof Record) {
						records.add((Record) obj);
					}
				}
				return records;
			}
		}
		return null;
	}

}
